package com.xhui.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhui.reggie.common.R;
import com.xhui.reggie.dto.SetmealDto;
import com.xhui.reggie.entity.Category;
import com.xhui.reggie.entity.Dish;
import com.xhui.reggie.entity.Setmeal;
import com.xhui.reggie.entity.SetmealDish;
import com.xhui.reggie.service.CategoryService;
import com.xhui.reggie.service.DishService;
import com.xhui.reggie.service.SetmealDishService;
import com.xhui.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/setmeal")
@Api(tags = "套餐相关接口")
public class SetmealController {

    @Resource
    private SetmealService setmealService;
    
    @Resource
    private SetmealDishService setmealDishService;

    @Resource
    private DishService dishService;

    @Resource
    private CategoryService categoryService;

    /**
     * 新增套餐信息
     * @param setmealDto 前端传递过来的套餐信息
     * @return 是否成功的提示
     */
    @PostMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    @ApiOperation(value = "新增套餐接口")
    public R<String> save(@RequestBody SetmealDto setmealDto){
        // 实现过程中
        System.out.println("setmealDto = " + setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    /**
     * 获取套餐页面信息
     * @param page 页码
     * @param pageSize 页面条数
     * @param name 模糊字段套餐名称
     * @return 返回页面信息
     */
    @GetMapping("/page")
    @ApiImplicitParams({
          @ApiImplicitParam(name = "page",value = "页码", required = true),
          @ApiImplicitParam(name = "pageSize",value = "每页条数", required = true),
          @ApiImplicitParam(name = "name",value = "套餐名称", required = false),
    })
    public R< Page<Setmeal> > page(int page, int pageSize, String name){
        // 1. 查询setmeal表
        // 1.1 构造分页对象
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        // 1.2 构造分页条件
        LambdaQueryWrapper<Setmeal> wrapper= new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name), Setmeal::getName, name);
        wrapper.orderByDesc(Setmeal::getUpdateTime);

        // 将查询到的条件封装到setmealPage中的records中去
        setmealService.page(setmealPage, wrapper);
        // 2. 查询category表。
        // 对records中的categoryName根据categoryId进行设置套餐名称
        List<Setmeal> records = setmealPage.getRecords();
        records.stream().forEach(setmeal -> {
            Category category = categoryService.getById(setmeal.getCategoryId());
            setmeal.setCategoryName(category.getName());
        });
        return R.success(setmealPage);
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal 封装条件: categoryId setmeal.status
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R< List<Setmeal> > list(Setmeal setmeal){
        Long categoryId = setmeal.getCategoryId();
        Integer status = setmeal.getStatus();

        // 1. 查询setmeal表，获取未被禁用的套餐信息 --> setmeal表
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, categoryId);
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus, status);
        List<Setmeal> queriedSetmeals = setmealService.list(setmealLambdaQueryWrapper);
        if (queriedSetmeals == null) {
            // 此套餐类型已被禁用
            return R.success(null);
        }

        //错以为查询套餐中的菜品
       /* // 2.获取到一个套餐的多个类型，通过多个setmeal_id --> setmeal_dish表
        List<Long> setmealIds = new ArrayList<>();
        queriedSetmeals.stream().forEach( item -> {
            setmealIds.add(item.getId());
        });
        LambdaQueryWrapper<SetmealDish> setmealDishWrapper = new LambdaQueryWrapper<>();
        setmealDishWrapper.in(SetmealDish::getSetmealId ,setmealIds);
        List<SetmealDish> setmealDishes = setmealDishService.list(setmealDishWrapper);

        // 3. 获取套餐中的菜品详细信息 --> dish表
        ArrayList<Long> dishIds = new ArrayList<>();
        setmealDishes.stream().forEach(setmealDish -> {
            dishIds.add(setmealDish.getDishId());
        });

        System.out.println(dishIds);
        List<Dish> dishes = dishService.listByIds(dishIds);*/

        return R.success(queriedSetmeals);
    }

    /**
     * 删除若干个套餐(一个或多个)
     * @param ids 套餐ids
     * @return 是否成功
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){
        System.out.println("ids = " + ids);
        setmealService.removeWithDish(ids);
        return R.success("套餐删除成功");
    }

    /**
     *  修改启动动态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids){
        System.out.println("status = " + status);
        setmealService.updateStatus(status, ids);
        return R.success("修改套餐状态成功");
    }
}
